package com.miguo.component;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.app.AppHelper;
import com.miguo.bean.AdvertBean;
import com.miguo.ui.R;
import com.miguo.utils.Options;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * File Names: AdvertGridLayout
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-07-17
 * Time: 16:42
 */
public class AdvertGridView extends RelativeLayout implements View.OnClickListener {

    private Context mContext;
    private Activity mActivity;
    private LinearLayout[] mAdvertLayout = new LinearLayout[5];// 五个分类广告
    private List<AdvertBean> mAdvertList;

    public AdvertGridView(Context context) {
        this(context, null);
    }

    public AdvertGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AdvertGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        LayoutInflater.from(mContext).inflate(R.layout.layer_home_head_grid_advert, this);

        // 五个分类广告
        mAdvertLayout[0] = (LinearLayout) findViewById(R.id.advert_list_1);
        mAdvertLayout[0].setOnClickListener(this);
        mAdvertLayout[1] = (LinearLayout) findViewById(R.id.advert_list_2);
        mAdvertLayout[1].setOnClickListener(this);
        mAdvertLayout[2] = (LinearLayout) findViewById(R.id.advert_list_3);
        mAdvertLayout[2].setOnClickListener(this);
        mAdvertLayout[3] = (LinearLayout) findViewById(R.id.advert_list_4);
        mAdvertLayout[3].setOnClickListener(this);
        mAdvertLayout[4] = (LinearLayout) findViewById(R.id.advert_list_5);
        mAdvertLayout[4].setOnClickListener(this);
    }

    public void setList(List<AdvertBean> advert) {
        if (advert != null && advert.size() >= 5) {
            mAdvertList = advert;
            for (int i = 0; i <= 4; i++) {
                TextView adTitle = (TextView) mAdvertLayout[i].getChildAt(0);
                adTitle.setText(advert.get(i).getTitle());
                TextView adSubject = (TextView) mAdvertLayout[i].getChildAt(1);
                adSubject.setText(Html.fromHtml(advert.get(i)
                        .getSubject()));
                ImageView adImage = (ImageView) mAdvertLayout[i].getChildAt(2);
                ImageLoader.getInstance().displayImage(advert.get(i).getItem_pic_url(),
                        adImage, Options.getRoundedOptions());
                mAdvertLayout[i].setId(i);
            }
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        AppHelper.startActivityByType(mActivity, mAdvertList.get(v.getId()));
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            setList(mAdvertList);
            return;
        }
    }
}
